package org.apache.nutch.web;

import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DQueryServlet extends HttpServlet
{

	@Override
	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doPost(arg0, arg1);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		ArrayList li = new ArrayList();
		String dQueryName = req.getParameter("dqueryname");
		System.out.println(dQueryName);
		for(int i = 0; i < 2650; i++)
		{
			URL url = new URL(dQueryName + Integer.toString(i));
			URLConnection connection = url.openConnection();
			InputStream is = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String line;
			StringBuffer pageBuffer = new StringBuffer();
			while ((line = reader.readLine()) != null)
			{
				pageBuffer.append(line);
			}
			String pageContent = pageBuffer.toString();
			if(pageContent.indexOf("Research involves") != -1)
			{
				li.add(url.toString());
				System.out.println(url.toString());
				pageContent = pageContent.replaceAll("\"", "'");
				writeIntoMysql(dQueryName + Integer.toString(i), pageContent);
			}
		}
		
		req.setAttribute("dqueryresult", li);
		req.getRequestDispatcher("dquery.jsp").forward(req, res);
		return;
	}

	private void writeIntoMysql(String url, String pageContent)
	{
		// TODO Auto-generated method stub
		Connection conn = null;
		Statement stmt = null;
		
		try
		{
			// Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");

			Properties info = new Properties();
			info.setProperty("proxool.maximum-connection-count", "30");
			info.setProperty("proxool.house-keeping-test-sql", "select sysdate from dual");
			info.setProperty("user", "root");
			info.setProperty("password", "111111");
			String alias = "ResumeMysql";
			String driverClass = "org.gjt.mm.mysql.Driver";
			String url1 = "proxool." + alias + ":" + driverClass + ":" + "jdbc:mysql://202.96.25.35:3306/searchengine";

			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			conn = DriverManager.getConnection(url1, info);
			stmt = conn.createStatement();
			boolean bol = stmt.execute("insert into spidercollect values(NULL, \"" + new String((url).getBytes(), "latin1") + "\", \"" + new String(pageContent.getBytes(), "latin1") + "\", " + 60 + ", \"1\", \"\", \"\", \"\", \"\", \"\", \"\", \"\", \"\")");
			
			stmt.close();
			conn.close();
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			return;
		}
		catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
